//
//  DDStatusCellTopView.swift
//  SinaMicroBlogSwift2
//
//  Created by dqw on 15/8/3.
//  Copyright © 2015年 itcast. All rights reserved.
//

import UIKit

import SDWebImage

class DDStatusCellTopView: UIView {

    var status: DDStatus? {
        
        didSet {
            if let imageUrl = status?.user?.imageUrl {
                iconView.sd_setImageWithURL(imageUrl)
            }
            nameLabel.text = status?.user?.name ?? ""
            timeLabel.text = NSDate.SinaDate(string: status?.created_at)!.dateDescription
            sourceLabel.text = status?.source ?? ""
            vipIconView.image = status?.user?.vipImage
            memberIconView.image = status?.user?.memberImage
        }
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        addAndLayoutSubviews()
    }

    // MARK: - 添加并布局各子控件。网络现在的图片要先设置size，从本地加载的图片可以不设置size。重点。
    private func addAndLayoutSubviews() {
        
        addSubview(separateView)
        addSubview(iconView)
        addSubview(nameLabel)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(memberIconView)
        addSubview(vipIconView)
        
        
        separateView.ff_AlignInner(type: ff_AlignType.TopLeft, referView: self, size: CGSize(width: UIScreen.mainScreen().bounds.width, height: 10))
       iconView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: separateView, size: CGSize(width: 35, height: 35), offset: CGPoint(x: DDStatusCellMargin, y: DDStatusCellMargin))
        nameLabel.ff_AlignHorizontal(type: ff_AlignType.TopRight, referView: iconView, size: nil, offset: CGPoint(x: DDStatusCellMargin, y: 0))
        timeLabel.ff_AlignHorizontal(type: ff_AlignType.BottomRight, referView: iconView, size: nil, offset: CGPoint(x: DDStatusCellMargin, y: 0))
        sourceLabel.ff_AlignHorizontal(type: ff_AlignType.CenterRight, referView: timeLabel, size: nil, offset: CGPoint(x: DDStatusCellMargin, y: 0))
        memberIconView.ff_AlignHorizontal(type: ff_AlignType.TopRight, referView: nameLabel, size: nil, offset: CGPoint(x: DDStatusCellMargin, y: 0))
        vipIconView.ff_AlignInner(type: ff_AlignType.BottomRight, referView: iconView, size: nil, offset: CGPoint(x: DDStatusCellMargin, y: DDStatusCellMargin))
        
    }
    
    // MARK: - 懒加载各子控件。
    private lazy var separateView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor(white: 0.8, alpha: 1.0)
        return view
    }()
    private lazy var iconView: UIImageView = UIImageView()
    // 2. 姓名
    private lazy var nameLabel: UILabel = UILabel(color: UIColor.darkGrayColor(), fontSize: 14)
    // 3. 时间标签
    private lazy var timeLabel: UILabel = UILabel(color: UIColor.orangeColor(), fontSize: 9)
    // 4. 来源标签
    private lazy var sourceLabel: UILabel = UILabel(color: UIColor.lightGrayColor(), fontSize: 9)
    // 5. 会员图标
    private lazy var memberIconView: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership_level1"))
    // 6. vip图标
    private lazy var vipIconView: UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))

    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
